package com.ysjy.course.controller;

import com.github.pagehelper.PageInfo;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.PromptMessage;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.common.utils.StringUtil;
import com.ysjy.course.model.OrgCourse;
import com.ysjy.course.model.YsTrusteeship;
import com.ysjy.course.service.CourseService;
import com.ysjy.course.service.TrusteeshipService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(description = "托管服务管理")
@RequestMapping("/trusteeship/manage")
@Slf4j
public class TrusteeshipManageController extends BaseController {

    @Autowired
    private TrusteeshipService trusteeshipService;

    @ApiOperation(value = "获取托管服务分页列表")
    @RequestMapping(value = "getTrusteeshipListByPage", method = RequestMethod.GET)
    public ResultDTO<PageInfo<YsTrusteeship>> getTrusteeshipListByPage(
            @ApiParam(value = "机构ID", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
            @ApiParam(value = "名称（模糊搜索）", required = false) @RequestParam(name = "title", required = false) String title,
            @ApiParam(value = "上架状态：0未上架 1上架", required = false) @RequestParam(name = "shelvesStatus", required = false) Integer shelvesStatus,
            @ApiParam(value = "页码", required = true, defaultValue = "1") @RequestParam(name = "pageNo", required = true, defaultValue = "1") Integer pageNo,
            @ApiParam(value = "每页条数", required = true, defaultValue = "10") @RequestParam(name = "pageSize", required = true, defaultValue = "10") Integer pageSize) {
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            Map<String, Object> params = new HashMap<>();
            if (!StringUtil.isEmpty(title)) params.put("title", title);
            params.put("shelvesStatus", shelvesStatus);
            params.put("orgId", orgId);
            PageInfo<YsTrusteeship> pageInfo = trusteeshipService.getListByPage(params, pageNo, pageSize);
            return ResultDTO.successResult(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "获取托管服务列表(选择使用，例添加班级时)")
    @RequestMapping(value = "getTrusteeshipList", method = RequestMethod.GET)
    public ResultDTO<List<YsTrusteeship>> getTrusteeshipList(
            @ApiParam(value = "机构ID", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
            @ApiParam(value = "报名状态 0未开始、1报名中、2已结束", required = false) @RequestParam(name = "signUpStatus", required = false) Integer signUpStatus,
            @ApiParam(value = "上架状态：0未上架 1上架", required = false) @RequestParam(name = "shelvesStatus", required = false) Integer shelvesStatus) {
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            Map<String, Object> params = new HashMap<>();
            params.put("shelvesStatus", shelvesStatus);
            params.put("signUpStatus", signUpStatus);
            params.put("orgId", orgId);
            List<YsTrusteeship> pageInfo = trusteeshipService.getListByMapParams(params);
            return ResultDTO.successResult(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "获取托管服务详情")
    @RequestMapping(value = "getTrusteeship", method = RequestMethod.GET)
    public ResultDTO<YsTrusteeship> getTrusteeship(
            @ApiParam(value = "唯一标识", required = true) @RequestParam(name = "uniqueId", required = true) String uniqueId) {
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            YsTrusteeship data = trusteeshipService.getTrusteeshipByUniqueId(uniqueId,null);
            if (data != null) return ResultDTO.successResult(data);
            else return ResultDTO.errorResult(PromptMessage.LOAD_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "添加 or 编辑托管服务",notes = "{\"title\":\"暑期托管1\",\"logo\":\"https://qiniu.childrenonline.cn/Fin5cd36X2FCZOPEaNhU0qZCpq-x\",\"type\":3,\"tagId\":5,\"siteId\":5,\"gradeIds\":\"47,48\",\"gradeNames\":\"八年级,九年级\",\"content\":\"服务内容服务内容服务内容\",\"amount\":1,\"serviceYear\":\"2022\",\"maxSignUpNum\":99,\"classTime\":\"星期三 09:00~10:00,星期五 09:00~10:00\",\"startTime\":1653463992769,\"endTime\":1653463992769,\"signUpStartTime\":1653463992769,\"signUpEndTime\":1653463992769,\"orgIds\":\"1452\",\"orgNames\":\"紫城中学\"}")
    @RequestMapping(value = "putTrusteeship", method = RequestMethod.POST)
    public ResultDTO<Boolean> putTrusteeship(
            @Valid @RequestBody YsTrusteeship data){
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            return trusteeshipService.putTrusteeship(data);
        } catch (Exception e) {
            e.printStackTrace();
//            return ResultDTO.errorResult(PromptMessage.INFO_IS_NULL);
            return ResultDTO.errorResult(e.getMessage());
        }
    }

    @ApiOperation(value = "删除托管服务")
    @RequestMapping(value = "delTrusteeship", method = RequestMethod.POST)
    public ResultDTO<Boolean> delTrusteeship(
        @ApiParam(value = "唯一标识", required = true) @RequestParam(name = "id", required = true) Long id){
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            return trusteeshipService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.errorResult(PromptMessage.INFO_IS_NULL);
        }
    }

    @ApiOperation(value = "批量上下架托管服务")
    @RequestMapping(value = "changeTrusteeshipShelvesStatus", method = RequestMethod.POST)
    public ResultDTO changeTrusteeshipShelvesStatus(
            @ApiParam(value = "id(多个,号隔开)", required = true) @RequestParam(name = "ids", required = true) String ids,
            @ApiParam(value = "上架状态：0未上架 1上架", required = true) @RequestParam(name = "shelvesStatus", required = true) Integer shelvesStatus
    ){
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            trusteeshipService.changeShelvesStatus(ids,shelvesStatus);
            return ResultDTO.successResult();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }


}